package architecture;

//DataMemory
public class DataMemory{
	private char memory[]=new char[1024];
	MyInt address;
	MyInt write_data;
	MyInt read_data;
	MyInt memWrite;
	MyInt memRead;
	void initMem () {
		for(int address=0;address<1024;++address){
			putByte(address,0);
		}
	}
	int getWord (int address) {
		if((address < 0) || (address > 1020)) {
			System.out.println("invalid address in DataMemory!");
		}
		return
			(memory[address] << 24) +
			((memory[address+1] << 16) & 0xff0000) +
			((memory[address+2] << 8) & 0xff00) +
			((memory[address+3]) & 0xff);
	}
	void putByte( int address, int value )
	{
		if ((address < 0) || (address > 1020)) {
			System.out.println("invalid address in DataMemory!");
		}
		memory[address] = (char)(value);
	}
	int getByte (int address) {
		int n ;
		if ((address < 0) || (address > 1023)) {
			System.out.println("invalid address in DataMemory!");
		}
		n = (int)memory[address] ;
		return n ;
	}
	void putWord (int address, int value) {
		if ((address < 0) || (address > 1020)) {
			System.out.println("invalid address in DataMemory!");
		}
		memory[address] = (char)(value >> 24);
		memory[address+1] = (char)((value & 0xff0000) >> 16);
		memory[address+2] = (char)((value & 0xff00) >> 8);
		memory[address+3] = (char)(value & 0xff);
	}
	void clockEvent(){
		assert((memWrite.i&memRead.i)==0);
		switch(memWrite.i){
		case 0:
			break;
		case 1:
			putWord(address.i,write_data.i);
			break;
		}
		switch(memRead.i){
		case 0:
			break;
		case 1:
			read_data.i=getWord(address.i);
			break;
		}
	}
	void dump(){
		System.out.println("--------");
		System.out.println("DataMemory:");
		System.out.println("address:"+address);
		System.out.println("write_data:"+write_data);
		System.out.println("read_data:"+read_data);
		System.out.println("memWrite:"+memWrite);
		System.out.println("memRead:"+memRead);
		System.out.println("--------");
	}
}